package;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.Shape;
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.Responder;
import flash.Lib;

typedef PlayerData =
{
	var id : Int;
	var name : String;
	var x : Float;
	var y : Float;
	var rotation : Float;
	var health : Float;
}
class Player extends Sprite
{
	public var data:PlayerData;
	private var s : Shape;
	private var controller : PlayerControl;
	public var fireEnabled:Bool;
	public function new(data:PlayerData, control:Bool)
	{
		super();
		s = new Shape();
		addChild(s);
		draw(control);
		setData(data);
		if(control)
			controller = new PlayerControl(this);
		fireEnabled=true;
	}
	public function cleanup()
	{
		controller.cleanup();
		controller = null;
		data = null;
		s.graphics.clear();
	}
	function draw(control:Bool)
	{
		s.graphics.clear();
		s.graphics.lineStyle(2, control?0x000000:0xFFFFFF);
		s.graphics.beginFill(control?0xFF0000:0x00000, 1);
		s.graphics.drawCircle(20,20,20);
		s.graphics.endFill();
		s.graphics.moveTo(20,0);
		s.graphics.lineTo(20,20);
		s.x = s.y = -20;
	}
	public function setData(data:PlayerData)
	{
		this.data = data;
		name = data.name;
		x = data.x;
		y = data.y;
		rotation = data.rotation;
	}
}
class PlayerControl
{
	var maxSpeed:Float;
	var rotationSpeed:Float;
	var thrust:Float;
	var decay:Float;
	var speed:Float;
	var speedX:Float;
	var speedY:Float;
	var up:Bool;
	var left:Bool;
	var right:Bool;
	
	var player : Player;

	public function new(p:Player)
	{
		player = p;
		maxSpeed = 5;
		rotationSpeed = 5;
		thrust = .5;
		decay = .97;
		speed = 0;
		speedX = 0;
		speedY = 0;

		setEnabled(true);
	}
	public function cleanup()
	{
		setEnabled(false);
		player=null;
	}
	public function setEnabled(value:Bool):Void
	{
		if(value)
		{
			Lib.current.stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyEvent);
			Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyEvent);
		}
		else
		{
			Lib.current.stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			Lib.current.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyEvent);
			Lib.current.stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyEvent);
		}
	}
	function onKeyEvent(event:KeyboardEvent):Void
	{
		var value = event.type == KeyboardEvent.KEY_DOWN;
		switch( event.keyCode )
		{
		case Keyboard.UP: up = value;
		case Keyboard.LEFT: left = value;
		case Keyboard.RIGHT: right = value;
		case Keyboard.SPACE: if(player.fireEnabled){player.fireEnabled=false; fire();};
		}
	}
	private function enterFrameHandler(event:Event):Void
	{
		if(right)
			player.data.rotation += rotationSpeed;
		if(left)
			player.data.rotation -= rotationSpeed;
		if(up)
		{
			speedX += thrust * Math.sin(player.data.rotation * (Math.PI / 180));
			speedY += thrust * Math.cos(player.data.rotation * (Math.PI / 180));
		}
		else
		{
			speedX *= decay;
			speedY *= decay;
		}
		speed = Math.sqrt((speedX * speedX) + (speedY * speedY));
		if(speed > maxSpeed)
		{
			speedX *= maxSpeed/speed;
			speedY *= maxSpeed/speed;
		}
		if(Math.abs(speedY) > 0.05)
		{
			player.data.y -= speedY;
		}	
		if(Math.abs(speedX) > 0.05)
			player.data.x += speedX;

		if(player.data.x < 0) player.data.x=0;
		if(player.data.x > 800) player.data.x=800;
		if(player.data.y < 0) player.data.y=0;
		if(player.data.y > 600) player.data.y=600;
		
		if(left || right || up || Math.abs(speedX) > 0.05 || Math.abs(speedY) > 0.05)
			player.dispatchEvent(new Event("move"));
	}
	function fire()
	{
		player.dispatchEvent(new Event("fire"));
	}
}